/**
 * 载入项目引导文件
 */
require('./bootstrap');

//初始化树结构
let initTree = (group_id = 0) => {
    let tree_data = {};
    utils.ajaxGet(page_params.url_list, {}, (response, data) =>{
        if (response.data.treeList){
            tree_data = response.data.treeList;
            $('#tree').treeview({data: tree_data,levels:2,showTags:!0,collapseIcon:'fa fa-chevron-down',expandIcon:'fa fa-chevron-right'});
            //绑定选中事件
            $('#tree').on('nodeSelected', function(event, data) {
                asxBox = layer.open({
                    type: 1,
                    title: '菜单操作',
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    skin: 'layui-layer-molv', //加上边框
                    area: ['520px', '150px'], //宽高
                    content: $('#box_ask')
                });
                nowNode = data;
            });
        }
    });
};

let box;
let asxBox;
let nowNode;


$(document).ready(()=>{
    //切换树结构
    initTree();
    //搜索重新刷新菜单
    $("#btn-search").on('click', function () {
        initTree();
    });
    //添加顶级标签
    $("#btn-add").on('click', () => {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_add);
        $("#box-form input[name='role_id']").val(0);
        $("#box-form input[name='parent_id']").val(0);
        box = layer.open({
            type: 1,
            title: '添加【顶级标签】',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '280px'], //宽高
            content: $('#box-form')
        });
    });
    //添加子角色
    $("#btn-add-child").on('click', function () {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_add);
        $("#box-form input[name='tag_id']").val('');
        $("#box-form input[name='parent_id']").val(nowNode.tag_id);
        box = layer.open({
            type: 1,
            title: '添加【' + nowNode.display_name + '】子角色',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '280px'], //宽高
            content: $('#box-form')
        });
    });
    //编辑角色
    $("#btn-edit").on('click', function () {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_edit);
        $("#box-form input[name='tag_id']").val(nowNode.tag_id);
        $("#box-form input[name='parent_id']").val(nowNode.parent_id);
        $("#box-form input[name='tag_name']").val(nowNode.tag_name);
        $("#box-form input[name='order_num']").val(nowNode.order_num);
        box = layer.open({
            type: 1,
            title: '编辑角色',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '280px'], //宽高
            content: $('#box-form')
        });
    });
    //提交表单
    $('#box-form .btn-sub').on('click', function () {
        utils.ajaxSubmitForm($("#box-form form"), '', {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                layer.close(asxBox);
                initTree();
            }
        });
    });
    //删除
    $("#btn-del").on('click', function () {
        let tag_id = 0;
        if (nowNode) {
            tag_id = nowNode.tag_id;
        }
        utils.confirm(page_params.url_del, {'id': tag_id}, function (response, postdata) {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(asxBox);
                initTree();
            }
        });
    });
    //同步标签数据
    $("#btn-sync").on('click', function () {
        utils.ajaxPost(page_params.url_sync,{},(response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                layer.close(asxBox);
                initTree();
            }
        });
    });
});